package ca.etsmtl.log121.tp3.view;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import ca.etsmtl.log121.tp3.controller.ApplicationController;
import ca.etsmtl.log121.tp3.model.PerspectiveModel;

public class PerspectiveView extends JPanel implements Observer, MouseListener, MouseMotionListener, MouseWheelListener {
	/**
	 * TODO
	 */
	private static final long serialVersionUID = 2039071401661090129L;

	private PerspectiveModel perspective;
	private ApplicationController controller;
	private int mouseX, mouseY, posX, posY;

	public PerspectiveView(PerspectiveModel perspective, ApplicationController controller) {
		this.perspective = perspective;
		this.controller = controller;

		setBackground(Color.WHITE);
		setBorder(new LineBorder(getBackground().darker()));

		addMouseListener(this);
		addMouseMotionListener(this);
		addMouseWheelListener(this);
	}

	@Override
	public void paintComponent(Graphics g){
		super.paintComponent(g); // Paint background

		g.drawImage(perspective.getImage(), perspective.getPosX(), perspective.getPosY(), perspective.getImageWidth(), perspective.getImageHeight(), null);
	}

	@Override
	public void update(Observable o, Object arg) {
		repaint();
	}

	@Override
	public void mouseClicked(MouseEvent e) {
	}

	@Override
	public void mousePressed(MouseEvent e) {
		mouseX = e.getX();
		mouseY = e.getY();
		posX = perspective.getPosX();
		posY = perspective.getPosY();
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		int[] mousePos = {mouseX,mouseY};
		int[] posBase = {posX,posY};
		controller.handleEvent(e,mousePos,posBase);
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

	@Override
	public void mouseDragged(MouseEvent e) {
//		perspective.setPosX(mouseX - e.getX()); // XXX: This is fun like shit. You need to try it
//		perspective.setPosY(mouseY - e.getY()); // XXX: This is fun like shit. You need to try it
//		perspective.setPosX((e.getX() - mouseX) + posX);
//		perspective.setPosY((e.getY() - mouseY) + posY);
		perspective.setPosXPosY((e.getX() - mouseX) + posX, (e.getY() - mouseY) + posY);
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		setCursor(new Cursor(Cursor.HAND_CURSOR));
//		setCursor(new Cursor(Cursor.MOVE_CURSOR));
	}

	@Override
	public void mouseWheelMoved(MouseWheelEvent e) {
		controller.handleEvent(e);
//		perspective.setZoomFactor(perspective.getZoomFactor() + e.getWheelRotation() * PerspectiveModel.ZOOMFACTOR_MULTIPLIER * -1);
	}

	public PerspectiveModel getPerspective() {
		return perspective;
	}
}